<template>
    <div class="category">
        <Header></Header>
        <div class="mb20 fr mr30">
            <el-button type="primary" @click="openDialog">添加分类</el-button>
        </div>
        <el-table
                :data="categoryList"
                border
                style="width: 100%">
            <el-table-column
                    prop="_id"
                    label="编号">
            </el-table-column>
            <el-table-column
                    prop="title"
                    label="名称">
            </el-table-column>
            <el-table-column
                    label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row._id)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row._id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>

        <el-dialog title="添加分类" :visible.sync="dialogAdd" @close="handleClose">
            <el-form :model="form" label-width="180">
                <el-form-item label="活动名称">
                    <el-input v-model="form.title" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAdd = false">取 消</el-button>
                <el-button type="primary" @click="handleAdd()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import Header from "../components/Header"
    export default {
        name: "ArticleTitle",
        components:{
            Header
        },
        data(){
            return{
                categoryList:[],
                form:{
                    title:''
                },
                dialogAdd:false,
                currentPage:1,
                pageSize:10,
                total:0
            }
        },
        mounted() {
            this.getList()
        },
        methods:{
            getList(){
                this.post(this.$api.articleTitleModel.articleTitleList,{
                    currentPage:this.currentPage,
                    pageSize:this.pageSize
                })
                    .then(res=>{
                        this.categoryList = res.result.list
                        this.total = res.result.total
                    })
            },
            handleClose(){
                this.form={}
            },
            openDialog(){
                this.dialogAdd = true
            },
            handleAdd(){
                this.post(this.$api.articleTitleModel.articleTitleAdd,this.form)
                    .then(res=>{
                        console.log(res)
                        this.categoryList.push(res.result)
                        this.dialogAdd = false
                        // this.getList()
                    })
            },
            handleDelete(id){
                let userId =id
                this.$delete(`category/${id}`)
                    .then(res=>{
                        console.log(res)
                        // this.getList()
                        this.categoryList.forEach((item,index)=>{
                            if (item._id==userId){
                                this.categoryList.splice(index,1)
                                this.total-=1
                            }
                        })
                    })
            },
            handleSizeChange(val){
                this.pageSize = val
                this.getList()
            },
            handleCurrentChange(val){
                this.currentPage = val
                this.getList()
            }
        }
    }
</script>

<style lang="stylus" scoped>
    .category
        width 100%

</style>
